﻿using SuperMoney.Datas;
using SuperMoney.Datas.DbModels;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Transactions;

namespace SuperMoney
{
	/// <summary>
	/// 积分交易服务。
	/// </summary>
	public class IntegralTradeService
	{
		/// <summary>
		/// 广告记录服务实例。
		/// </summary>
		public static IntegralTradeService Instance = new IntegralTradeService();

		public void Create(string deviceId, IntegralTradeCategory category, int integral, string name, IntegralTradeAssociation association, long associationId)
		{
			IntegralTrade integralTrade = new IntegralTrade();
			integralTrade.DeviceId = deviceId;
			integralTrade.Category = (int)category;
			integralTrade.Integral = integral;
			integralTrade.Name = name;
			integralTrade.Association = (int)association;
			integralTrade.AssociationId = associationId;

			using (TransactionScope tx = new TransactionScope())
			{
				IntegralTradeDbService.Instance.Create(integralTrade);
				if (integral != 0)
				{
					DeviceAccountDbService.Instance.AddIntegralBalance(deviceId, integral);
				}
				tx.Complete();
			}
		}
	}
}
